<?php
/*
	Joomler!.net WebLink Information Module - version 1.1.1 for 1.5.x
______________________________________________________________________
	License http://www.gnu.org/copyleft/gpl.html GNU/GPL
	Copyright(c) 2009 Joomler!.net All Rights Reserved.
	Comments & suggestions: http://www.joomler.net/
*/

/**
* @package		Joomla
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL
*/

defined('_JEXEC') or die('Restricted access');
require_once JPATH_SITE. DS. 'components'. DS. 'com_weblinks'. DS. 'helpers'. DS. 'route.php';

class JWebLink_Information
{

	function getInfo ($params)
	{

		$type = intval($params->get('displaytype', 0));
		$limit = "\n LIMIT " . intval($params->get('max', 10));

		$categories = trim($params->get('catid', ''));
		if (! empty($categories)) {
			$cats = explode(',', $categories);
			$temp = array(
			);
			foreach ($cats as $cat) {
				$cat = (int) $cat;
				if ($cat) {
					$temp[] = $cat;
				}
			}
			$categories = '';
			if (count($temp)) {
				$temp = implode(',', $temp);
				$categories = " AND c.id IN($temp)";
			}
		}

		$group = intval($params->get('group', 1));

		$catdesc = '';
		if ($group && intval($params->get('showcatdesc', 1))) {
			$catdesc = ', c.description AS catdescription';
		}
		$cattitle = '';
		if ($group && intval($params->get('showcattitle', 1))) {
			$cattitle = ', c.title AS cattitle';
		}
		$image = '';
		if (intval($group && $params->get('showcatimage', 1))) {
			$image = ", c.image";
		}

		switch ($type) {
			case 1:
				$order = "\n ORDER BY w.hits DESC, c.ordering ASC";
				break;
			case 2:
				$order = "\n ORDER BY RAND(), c.ordering ASC";
				break;
			case 3:
				$order = "\n ORDER BY w.ordering ASC, c.ordering ASC";
				break;
			case 4:
				$order = "\n ORDER BY w.ordering DESC, c.ordering ASC";
				break;
			case 5:
				$order = ' ORDER BY w.title, c.ordering ASC';
				break;
			case 6:
				$order = ' ORDER BY w.title DESC, c.ordering ASC';
				break;
			default:
				$order = "\n ORDER BY w.date DESC, c.ordering ASC";
		}

		//access
		$user = & JFactory::getUser();
		$aid = (int) $user->get('aid', 0);

		$db = &JFactory::getDBO();

		$query = "SELECT w.title, w.catid, w.url,"
		. "\n CASE WHEN CHAR_LENGTH(w.alias) THEN CONCAT_WS(':', w.id, w.alias) ELSE w.id END as slug,"
		. "\n CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(':', c.id, c.alias) ELSE c.id END as catslug,"
		. "\n w.description, w.date, w.hits$catdesc$cattitle$image"
		. "\n FROM #__weblinks AS w"
		. "\n INNER JOIN #__categories AS c ON c.id = w.catid"
		. "\n WHERE w.published = 1 AND w.archived = 0 AND approved = 1"
		. "\n AND c.published = 1 AND c.access <= $aid"
		. $categories . $order . $limit
		;
		$db->setQuery($query);
		$rows = $db->loadObjectList();

		if($db->getErrorNum()){
			JError::raiseError(500, $db->getErrorMsg());
		}

		if (count($rows) < 1) {
			return array();
		}

		$thumbAPI = 0;
		if ($params->get('showthumb', 1)) {
			$thumbAPI = intval($params->get('thumbapi', 1));
			$width = intval($params->get('thumbnailwidth', 30));
			$height = intval($params->get('thumbnailheight', 35));
			$snapcasacode = trim($params->get('snapcasacode'));
			$snapcasasize = $params->get('snapcasasize', 'S');
		}

		$temp = array(
		);
		if ($group) {
			foreach ($rows as $row) {
				$temp[$row->catid]['title'] = $row->cattitle;
				$temp[$row->catid]['description'] = $row->catdescription;
				$temp[$row->catid]['image'] = $row->image;
				$row->link = JRoute::_( WeblinksHelperRoute::getWeblinkRoute($row->slug, $row->catslug) );
				$row->thumbnail = '';
				JWebLink_Information::makeThumbnail($row, $thumbAPI, $width, $height, $snapcasacode, $snapcasasize);
				$temp[$row->catid]['list'][] = $row;
			}

			$temp['params'] = JWebLink_Information::getParams($params);
			return $temp;
		}

		foreach ($rows as $row) {
			$row->link = JRoute::_( WeblinksHelperRoute::getWeblinkRoute($row->slug, $row->catslug) );
			$row->thumbnail = '';
			JWebLink_Information::makeThumbnail($row, $thumbAPI, $width, $height, $snapcasacode, $snapcasasize);
			$temp[] = $row;
		}
		$temp['params'] = JWebLink_Information::getParams($params);
		return $temp;

	}

	function getParams ($params)
	{
		$temp = array(
		);
		$temp['title'] = trim($params->get('title', ''));
		$temp['width'] = intval($params->get('thumbnailwidth', 30));
		$temp['target'] = $params->get('target', '_blank');
		$temp['showthumb'] = intval($params->get('showthumb', 1));
		$temp['showCatDescription'] = intval($params->get('showcatdesc', 1));
		$temp['catImageWidth'] = intval($params->get('catimagewidth', 48));
		$temp['group'] = intval($params->get('group', 1));
		$temp['suffix'] = $params->get('moduleclass_sfx', '');
		$temp['class'] = 'weblinkinfo' . $temp['suffix'];
		$temp['showdesc'] = $params->get('showdesc', 1);
		$temp['showcattitle'] = $params->get('showcattitle', 1);
		$temp['hit'] = $params->get('hit', 1);
		return $temp;
	}

	function makeThumbnail (&$row, $thumbAPI, $width, $height, $snapcasacode, $snapcasasize)
	{
		if (! $thumbAPI) {
			return;
		}

		switch ($thumbAPI) {
			/**
			 * URL : $item->url
			 * Title : $item->title
			 */
			case 3:
				/**
				 * Simple API is japanese famous api.
				 * @link http://img.simpleapi.net/
				 */
				$src = 'http://img.simpleapi.net/small/' . $row->url;
				break;
			case 1:
				/**
				 * MozShot is japanese famous api.
				 * @link http://mozshot.nemui.org/
				 */
				$src = 'http://mozshot.nemui.org/shot/' . $width . 'x' . $height . '?' . $row->url;
				break;
			case 2: //SnapCasa
				/**
				 * SnapCasa
				 * @link http://snapcasa.com
				 * <img src="http://SnapCasa.com/Get.aspx?code=[your registerd code]&size=[size]&url=[url]" />
				 */

				if (! empty($snapcasacode)) {
					$src = "http://SnapCasa.com/Get.aspx?code=$snapcasacode&size=$snapcasasize&url=$row->url";
				}
				break;
		}

		if (! empty($src)) {
			$row->thumbnail = "<img src=\"$src\" alt=\"$row->title\" width=$width height=$height />";
		}
	}

	function styleOutput (& $params)
	{
		static $output = false;
		if ($params->get('outputstyle', 1) && ! $output) {
			$output = true;
			$config = & JFactory::getConfig();
			$caching = (int) $config->getValue('config.caching');
			$cache = (int) $params->get('cache');
			if ($caching && $cache) {
				$params->set('outputstyle', 2);
				return true;
			}
			$output = true;
		} else {
			return false;
		}
		return $output;
	}
}